package pro.shuangxi.model.yundisk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import mybatisplus.pojo.Yundisk;
import mybatisplus.service.IYundiskService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import pro.shuangxi.common.aop.annotation.Token;
import pro.shuangxi.common.vo.Result;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class YunDiskController {
    @Autowired
    IYundiskService yundiskService;
    @Autowired
    Environment environment;


    @RequestMapping("/getFiles.do")
    @Token
    public Result getDefFileList(Integer id,@RequestParam(value = "path",defaultValue = "") String path){
        if (StringUtils.isEmpty(path)) {
            Yundisk one = yundiskService.getOne(new QueryWrapper<Yundisk>().eq("userid", id));
            if (one == null) {
                one= new Yundisk();
                one.setUserid(id);
                one.setPath(getDefPath(id.toString()));
                yundiskService.save(one);
            }

            return Result.OK(getFiles(one.getPath()));
        }else {
            return Result.OK(getFiles(path));
        }
    }
    private List getFiles(String path){
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
            return new ArrayList();
        }
        File[] files = file.listFiles();
        if (files == null) {
            files=new File[]{};
        }
        List<File> fileList = Arrays.asList(files);
        List<HashMap> collect = fileList.stream().sorted(Comparator.comparing(File::lastModified)).map(f -> {
            HashMap map = new HashMap();
            map.put("name", f.getName());
            map.put("path", f.getAbsolutePath());
            map.put("type", f.isDirectory() ? "directory" : "file");
            map.put("size", String .format("%.2f",f.length()/1024d)+" KB");
            map.put("modifytime", DateFormatUtils.ISO_DATE_FORMAT.format(new Date(f.lastModified())));
            return map;
        }).collect(Collectors.toList());
        if (file.getParentFile() != null) {
            HashMap map = new HashMap();
            map.put("name", "返回上一级");
            map.put("path", file.getParentFile().getAbsolutePath());
            map.put("type",  "directory");
            map.put("size", "0 KB");
            map.put("modifytime", DateFormatUtils.ISO_DATE_FORMAT.format(new Date(file.lastModified())));
            collect.add(0,map);

        }
        if (file.getParentFile() != null) {
            HashMap map = new HashMap();
            map.put("name", "点击刷新");
            map.put("path", file.getAbsolutePath());
            map.put("type",  "directory");
            map.put("size", "0 KB");
            map.put("modifytime", DateFormatUtils.ISO_DATE_FORMAT.format(new Date(file.lastModified())));
            collect.add(0,map);

        }


        return collect;
    }

    private String getDefPath(String id) {
        if(StringUtils.isEmpty(id)){
            throw new RuntimeException("getDefPath的参数不能是空");
        }
       String workplace = environment.getProperty("workplace");
        if(StringUtils.isEmpty(workplace)) {
            throw new RuntimeException("workplace参数未配置，功能不可用");
        };
       String path= workplace + File.separator +"yundisk"+ File.separator + id;
        return path;
    }



}